import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      // home: CanvasTestDemo(),
      home: ArcExample(),
    );
  }
}

class ArcExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CustomPaint(
          size: Size(200, 200),
          painter: ProgressBar(progress: 0.5),
        ),
      ),
    );
  }
}

class ProgressBar extends CustomPainter {
  final double progress; // 0.0 到 1.0

  ProgressBar({required this.progress});

  @override
  void paint(Canvas canvas, Size size) {
    // 背景
    final backgroundPaint = Paint()
      ..color = Colors.grey[300]!
      ..style = PaintingStyle.fill;

    final bgOuter = RRect.fromLTRBAndCorners(
      0,
      0,
      size.width,
      20,
      topLeft: Radius.circular(10),
      topRight: Radius.circular(10),
      bottomLeft: Radius.circular(10),
      bottomRight: Radius.circular(10),
    );

    final bgInner = RRect.fromLTRBAndCorners(
      2,
      2,
      size.width - 2,
      18,
      topLeft: Radius.circular(8),
      topRight: Radius.circular(8),
      bottomLeft: Radius.circular(8),
      bottomRight: Radius.circular(8),
    );

    canvas.drawDRRect(bgOuter, bgInner, backgroundPaint);

    // 进度
    final progressPaint = Paint()
      ..color = Colors.blue
      ..style = PaintingStyle.fill;

    final progressWidth = (size.width - 4) * progress;

    final progressOuter = RRect.fromLTRBAndCorners(
      2,
      2,
      2 + progressWidth,
      18,
      topLeft: Radius.circular(8),
      topRight: Radius.circular(progressWidth > 8 ? 8 : 0),
      bottomLeft: Radius.circular(8),
      bottomRight: Radius.circular(progressWidth > 8 ? 8 : 0),
    );

    final progressInner = RRect.fromLTRBAndCorners(
      4,
      4,
      progressWidth,
      16,
      topLeft: Radius.circular(6),
      topRight: Radius.circular(progressWidth > 10 ? 6 : 0),
      bottomLeft: Radius.circular(6),
      bottomRight: Radius.circular(progressWidth > 10 ? 6 : 0),
    );

    if (progressWidth > 4) {
      canvas.drawDRRect(progressOuter, progressInner, progressPaint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
